<?php

namespace app\common\controller;

use app\api\controller\User;
use app\api\model\Member;
use think\cache\driver\Redis;
use think\Controller;
use think\Db;

/**
 * 渠道用户注册登录
 */
class ChannelUser extends Controller
{
    public $redis = '';
    public function initialize(){
        $this->redis = new Redis(getRedisConfig());
    }
    /**
     * 注册账号
     * @param int $channel_id 渠道id
     * @param int $channel_user_id 验证码
     * @param int $game_id 游戏id
     * @param int $package_id 游戏包id
     * @param string $package_name 游戏包名称
     * @param int $promote_id 推广员id
     * @param string $device 设备号
     * @param string $realname 姓名
     * @param string $idcard 身份证
     * @return array user_id,token
     * @since 2023/11/07
     * @author ZhangXuanNeng
     */
    public function getInfo($channel_id,$channel_user_id,$game_id,$package_id,$package_name,$promote_id,$device='',$realname='',$idcard='',$ext){
        try {
            $User = new User();
            $key = Env('order.prefix').$channel_id.'_'.$channel_user_id;
            $status = $this->redis->get($key);
            $data = array(
                'channel_id'        => $channel_id,
                'channel_user_id'   => $channel_user_id,
                'game_id'           => $game_id,
                'package_id'        => $package_id,
                'device'            => $device,
                'realname'          => $realname,
                'idcard'            => $idcard,
                'package_name'      => $package_name,
                'promote_id'        => $promote_id,
                'age_status'        => 1
            );
            $member = new Member();
            if(!$status){
                $userInfo = $member->getChildInfo(['channel_id'=>$channel_id,'channel_user_id'=>$channel_user_id]);
                if($userInfo){
                    $this->redis->set($key,$userInfo['id'], 30 * 86400);
                    $status = $userInfo['id'];
                }
            }
            $registerType = false;
            if(!$status){
                $data['username'] = randomkeys_(rand(6, 10));
                $data['password'] = randomkeys_(rand(6, 10));
                $data['avatar'] = Db::name('box_hot_avatar')->where('is_default',1)->value('icon');
                $res = $member->userRegister($data,1);
                if($res['code'] != 0){
                    return $res;
                }
                $status = $res['data'];
                $registerType = true;
                $this->redis->set($key,$res['data'], 30 * 86400);
            }
            $memberInfo = $member->getChildInfo(['id'=>$status]);
            if($registerType){
                $this->createRegisterLog($memberInfo,1,$ext);
            }
            $User->setLog($memberInfo,1);
            $yqsdkToken = $member->updateLogin_($memberInfo,$data['game_id']);
            $childKey = Env('order.prefix').$data['game_id'].'_'.$data['channel_id'].'_'.$data['channel_user_id'].'_'.$status;
            $childStatus = $this->redis->get($childKey);
            $registerChildType = false;
            if($childStatus){
                $child_user_id = $childStatus;
            }else{
                $child_user_status = $member->getChildInfo(['pid'=>$status,'game_id'=>$data['game_id']]);
                if($child_user_status){
                    $child_user_id = $child_user_status['id'];
                    $this->redis->set($childKey,$child_user_status['id'],30 * 86400);
                }else{
                    $childData = array(
                        'pid'       => $status,
                        'username'  => randomkeys_(rand(6, 10)),
                        'package_id'=> $data['package_id'],
                        'package_name'=> $data['package_name'],
                        'promote_id'=> $data['promote_id'],
                        'nickname'  => '小号一',
                        'device'    => $data['device'],
                        'game_id'   => $data['game_id'],
                        'channel_id'=> $channel_id
                    );
                    $result = $member->userRegister($childData,1);
                    if($result['code'] != 0){
                        return $result;
                    }
                    $child_user_id = $result['data'];
                    $registerChildType = true;
                    $this->redis->set($childKey,$child_user_id,30 * 86400);
                }
            }
            $childUserInfo = $member->getChildInfo(['id'=>$child_user_id]);
            if($registerChildType){
                $this->createRegisterLog($childUserInfo,2,$ext);
            }
            $User->setLog($childUserInfo,2);
            return [
                'code'=>0,
                'msg'=>'登录成功',
                'data'=>['token'=>$yqsdkToken,'user_id'=>$child_user_id]
            ];
        }catch (\Exception $exception){
            return [
                'code'=>-4,
                'msg'=>$exception->getMessage(),
                'data'=>null
            ];
        }

    }

    public function createRegisterLog($data,$type = 1,$ext){
        $logData = array(
            'member_id'  => $data['id'],
            'package_id'=> $data['package_id'],
            'package_name'=> $data['package_name'],
            'type'      => $type,
            'device'    => $data['device'],
            'register_time'=> $data['create_time'],
            'promote_id'=> $data['promote_id'],
            'register_ip'=> $data['register_ip'],
            'promote_account'=> Db::name('user')->where('id',$data['promote_id'])->value('realname'),
            'platform'  => $ext['platform'],
            'sdk_version'=> $ext['sdk_version'],
            'network'   => $ext['network'],
            'net_isp'   => $ext['net_isp'],
            'sys_version'=> $ext['sys_version'],
            'device_name'=> $ext['device_name'],
            'port'      => $ext['port'],
            'idfa'      => $ext['idfa'],
            'caid'      => $ext['caid'],
            'android_id'=> $ext['android_id'],
            'oaid'      => $ext['oaid'],
            'bundle_id' => $ext['bundle_id']
        );
        $kafka = new Kafka();
        $className = 'cqyq_register';
        $kafka->logProducer($className,$logData);
    }

}
